<PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>YUI io HTTP Method Tests</title>
</head>

<body class="yui3-skin-sam">
<h1>YUI io HTTP Methods Tests</h1>

<script type="text/javascript" src="../../../build/yui/yui.js"></script>
<script type="text/javascript">

(function() {
    YUI({
        base: "../../../build/",
        filter: "debug",
        logExclude: {
            attribute: true,
            dom: true,
            node: true,
            event: true,
            base: true,
            widget: true,
            selector: true,
            io:true
        },
        useConsole: true
    }).use("test", "console", "io-base", function(Y) {

        var console = new Y.Console().render();

        var GET = new Y.Test.Case({
            name: 'HTTP GET',

            'testGET': function() {
            	var t = this;
            	this.handler = function() {
					Y.Assert.areSame(200, t.status);
            	};

				Y.io('get.php', {
					on: { success: function(i, o, a) {
							t.status = o.status;
							t.resume(t.handler);
						}
					}
				});

				this.wait(null, 1000);
            },

            'testGETWithData': function() {
            	var t = this, cb;
            	this.handler = function() {
            		Y.Assert.areSame('hello=world&foo=bar', t.response);
            	};
            	cb = {
					on: { success: function(id, o, a) {
							t.response = o.responseText;
							t.resume(t.handler);
						}
					}
            	};
				Y.io('get.php?hello=world&foo=bar', cb);
				this.wait(null, 1000);
            }
        });

        var DELETE = new Y.Test.Case({
            name: 'HTTP DELETE',

            'testDELETEWithData': function() {
            	var t = this, cb;
            	this.handler = function() {
            		Y.Assert.areSame('hello=world&foo=bar', t.response);
            	};
            	cb = {
					method: 'DELETE',
					on: { success: function(id, o, a) {
							t.response = o.responseText;
							t.resume(t.handler);
						}
					}
            	};
				Y.io('delete.php?hello=world&foo=bar', cb);
				this.wait(null, 1000);
            }
        });

        var HEAD = new Y.Test.Case({
            name: 'HTTP HEAD',

            'testHEAD': function() {
            	var t = this;
            	this.handler = function() {
            		if (t.headers) {
            			// IE, Safari, Opera all return HTTP response headers
						Y.Assert.isString(t.headers);
					}
					else {
						// Firefox 3 does not return anything except an HTTP
						// status of 0.
						Y.Assert.areSame(0, t.status);
					}
            	};

				Y.io('get.php', {
					method: 'HEAD',
					on: { complete: function(i, o, a) {
							t.status = o.status;
							t.headers = o.getAllResponseHeaders();
							t.resume(t.handler);
						}
					}
				});
				this.wait(null, 1000);
            }
        });

		// This test fails in Opera.
        var OPTIONS = new Y.Test.Case({
            name: 'HTTP OPTIONS',

            'testOPTIONS': function() {
            	var t = this;
            	this.handler = function() {
            		if (t.headers) {
						Y.Assert.areSame(200, t.status);
						Y.Assert.isString(t.headers);
						Y.log(t.headers);
					}
            	};

				Y.io('get.php', {
					method: 'OPTIONS',
					on: { complete: function(i, o, a) {
							t.status = o.status;
							t.headers = o.getAllResponseHeaders();
							t.resume(t.handler);
						}
					}
				});
				this.wait(null, 1000);
            }
        });

	    var POST = new Y.Test.Case({
			name: "HTTP POST",

			'testPOST': function() {
				var t = this;
				this.handler = function() {
					Y.Assert.areSame('world&bar', t.response, 'POST message and response do not match.');
				};

				Y.io('post.php', {
					method: 'POST',
					data: 'hello=world&foo=bar',
					on: { success: function(i, o, a) {
							t.response = o.responseText;
							t.resume(t.handler);
						}
					}
				});
				this.wait(null, 1000);
			},

			'testPOSTWithNoData': function() {
				var t = this;
				this.handler = function() {
					Y.Assert.areSame(200, t.status);
					Y.Assert.areSame(0, t.response, 'POST message and response do not match.');

				};

				Y.io('post.php', {
					method: 'POST',
					on: { success: function(i, o, a) {
							t.response =+ o.responseText;
							t.status = o.status;
							t.resume(t.handler);
						}
					}
				});
				this.wait(null, 1000);
        	}
    	});

	Y.Test.Runner.add(GET);
	Y.Test.Runner.add(HEAD);
	Y.Test.Runner.add(OPTIONS);
	Y.Test.Runner.add(POST);
	Y.Test.Runner.add(DELETE);
	Y.Test.Runner.run();
});
})();
</script>
</body>
</html>
